﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using _PLATFORM.DATA;

namespace _PLATFORM.WEB.Controllers
{ 
    public class SystemProductController : Controller
    {
        private eCommPlatformRepoEntities db = new eCommPlatformRepoEntities();
        private Guid UserID = _PLATFORM.DATA.Custom_Classes.User.UserID;

        //
        // GET: /SystemProduct/

        public ViewResult Index()
        {
            var system_product = db.SYSTEM_PRODUCT.Include(s => s.PRODUCT).Include(s => s.SYSTEM).Where(p => p.PRODUCT.USER_ID == UserID);
            return View(system_product.ToList());
        }

        //
        // GET: /SystemProduct/Details/5

        public ViewResult Details(decimal id)
        {
            SYSTEM_PRODUCT system_product = db.SYSTEM_PRODUCT.Find(id);
            return View(system_product);
        }

        //
        // GET: /SystemProduct/Create

        public ActionResult Create()
        {
            ViewBag.PRODUCT_ID = new SelectList(db.PRODUCTs.Where(p => p.USER_ID == UserID), "PRODUCT_ID", "NAME");
            ViewBag.SYSTEM_ID = new SelectList(db.SYSTEMs.Where(p => p.USER_ID == UserID), "SYSTEM_ID", "NAME");
            return View();
        } 

        //
        // POST: /SystemProduct/Create

        [HttpPost]
        public ActionResult Create(SYSTEM_PRODUCT system_product, FormCollection collection)
        {
            if (ModelState.IsValid)
            {
                var products = collection["PRODUCT_ID"].Split(',').ToList();

                foreach (var product in products)
                {
                    system_product = new SYSTEM_PRODUCT();
                    system_product.PRODUCT_ID = int.Parse(product);
                    system_product.SYSTEM_PRODUCT_ID = 0;
                    system_product.PRODUCT_COUNT = 1;
                    system_product.SYSTEM_ID = int.Parse(collection["SYSTEM_ID"]);
                    db.SYSTEM_PRODUCT.Add(system_product);
                    db.SaveChanges();
                }

                
                return RedirectToAction("Index");  
            }

            ViewBag.PRODUCT_ID = new SelectList(db.PRODUCTs, "PRODUCT_ID", "NAME", system_product.PRODUCT_ID);
            ViewBag.SYSTEM_ID = new SelectList(db.SYSTEMs, "SYSTEM_ID", "NAME", system_product.SYSTEM_ID);
            return View(system_product);
        }
        
        //
        // GET: /SystemProduct/Edit/5
 
        public ActionResult Edit(decimal id)
        {
            SYSTEM_PRODUCT system_product = db.SYSTEM_PRODUCT.Find(id);
            ViewBag.PRODUCT_ID = new SelectList(db.PRODUCTs, "PRODUCT_ID", "NAME", system_product.PRODUCT_ID);
            ViewBag.SYSTEM_ID = new SelectList(db.SYSTEMs, "SYSTEM_ID", "NAME", system_product.SYSTEM_ID);
            return View(system_product);
        }

        //
        // POST: /SystemProduct/Edit/5

        [HttpPost]
        public ActionResult Edit(SYSTEM_PRODUCT system_product)
        {
            if (ModelState.IsValid)
            {
                db.Entry(system_product).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.PRODUCT_ID = new SelectList(db.PRODUCTs, "PRODUCT_ID", "NAME", system_product.PRODUCT_ID);
            ViewBag.SYSTEM_ID = new SelectList(db.SYSTEMs, "SYSTEM_ID", "NAME", system_product.SYSTEM_ID);
            return View(system_product);
        }

        //
        // GET: /SystemProduct/Delete/5
 
        public ActionResult Delete(decimal id)
        {
            SYSTEM_PRODUCT system_product = db.SYSTEM_PRODUCT.Find(id);
            return View(system_product);
        }

        //
        // POST: /SystemProduct/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(decimal id)
        {            
            SYSTEM_PRODUCT system_product = db.SYSTEM_PRODUCT.Find(id);
            db.SYSTEM_PRODUCT.Remove(system_product);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}